package com.moshang.blog.controller.back.blog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moshang.blog.controller.base.BaseController;
import com.moshang.blog.core.annotation.SystemLog;
import com.moshang.blog.core.utils.PageUtil;
import com.moshang.blog.core.utils.RestResponse;
import com.moshang.blog.entity.TbMessage;
import com.moshang.blog.service.TbMessageService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author xww
 * Created on 2019/4/29.
 * 博客评论控制器类
 */
@Controller
@RequestMapping("/back/blog/comment")
public class BlogCommentController extends BaseController {

    private final  String  prefix="back/blog/comment/";
    @Autowired
    private TbMessageService tbMessageService;
    /**
     * 进入评论页面
     */
    @RequestMapping("list")
    public String showMenuList(){

        return  prefix+"list";
    }
    /**
     * 查询评论列表
     */
    @RequestMapping(value = "findList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "/findList", httpMethod = "POST", notes = "查询评论列表")
    public PageUtil<TbMessage> findList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                        @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                        ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        PageUtil<TbMessage> tbMessagePageUtil = new PageUtil<>();
        QueryWrapper<TbMessage> tbMessageQueryWrapper = new QueryWrapper<>();
        tbMessageQueryWrapper.eq("is_activited",0);
        if(!map.isEmpty()){
            String type = (String) map.get("type");
            if(StringUtils.isNotBlank(type)) {
                tbMessageQueryWrapper.eq("type", type);
            }
        }
        tbMessageQueryWrapper.orderByDesc("create_time");
        IPage<TbMessage> tbMessagePage = tbMessageService.page(new Page<>(page,limit),tbMessageQueryWrapper);
        tbMessagePageUtil.setCount((int) tbMessagePage.getTotal());
        tbMessagePageUtil.setData(tbMessagePage.getRecords());
        return tbMessagePageUtil;

    }
    @PostMapping("delete")
    @ResponseBody
    @SystemLog("删除评论/留言")
    @ApiOperation(value = "/delete", httpMethod = "POST", notes = "删除评论/留言")
    public RestResponse delete(@RequestParam(value = "id",required = false)Integer id){
        if(id == null || id == 0 || id == 1){
            return RestResponse.failure("参数错误");
        }
        tbMessageService.removeById(id);
        return RestResponse.success();
    }
    @PostMapping("batchDelete")
    @ResponseBody
    @SystemLog("批量删除角色")
    @ApiOperation(value = "/batchDelete", httpMethod = "POST", notes = "批量删除角色")
    public RestResponse batchDelete(@RequestBody List<TbMessage> messages){
        if(messages == null || messages.size() == 0){
            return RestResponse.failure("请选择需要删除的信息");
        }
        for (TbMessage tbMessage:messages){
        tbMessageService.removeById(tbMessage.getId());
        }

        return RestResponse.success();
    }
}
